package com.youlai.system.controller;

import cn.hutool.core.date.DateTime;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.AirFlight;
import com.youlai.system.service.AirFlightService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version 1.0
 * @Author Ricardo
 * @Date 2024/10/24 15:24
 * @注释
 */
@Tag(name = "10.航班信息接口")
@RestController
@RequestMapping("/api/v1/flight")
@RequiredArgsConstructor
public class AirFlightController {

    @Autowired
    private AirFlightService airFlightService;

    @PostMapping("/add")
    public Result addFlightInformation(AirFlight airFlight){
        DateTime time = DateTime.now();
        airFlight.setCreateTime(time.toString());
        if (airFlightService.saveOrUpdate(airFlight)){
            long flightId = airFlight.getFlightId();
            long planetId = airFlight.getPlaneId();

            //返回航班id和飞机Id
            return Result.success(flightId+planetId);
        }else {
            return Result.failed();
        }
    }
    @PutMapping("/delete")
    public Result deleteFlightInformation(long plane_id){
        if (airFlightService.removeById(plane_id)){
            return Result.success();
        }else {
            return Result.failed();
        }
    }

    //根据id查询航班信息
    @GetMapping("/select/{id}")
    public Result<List<AirFlight>> searchFlightInformationById(@PathVariable long id){
        if (airFlightService.list()!=null){
            return Result.success(airFlightService.list());
        }else {
            return Result.failed();
        }
    }
    @GetMapping("/select")
    public Result<List<AirFlight>> searchFlightInformation(){
        if (airFlightService.list()!=null){
            return Result.success(airFlightService.list());
        }else {
            return Result.failed();
        }
    }
}
